package com.cqupt.scanning.system.acl.controller;

import com.cqupt.scanning.system.acl.service.IndexService;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/acl/index")
public class IndexController {

    private IndexService indexService;

    @Autowired
    public IndexController(IndexService indexService) {
        this.indexService = indexService;
    }

    /**
     * 根据token获取用户信息
     * @return
     */
    @GetMapping("/userInfo")
    public R getUserInfo() {
        return indexService.getUserInfo();
    }


    /**
     * 根据当前用户来获取权限菜单
     * @return
     */
    @GetMapping("/getPermissionMenu")
    public R getPermissionMenu(){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return indexService.getPermissionMenu(username);
    }

    /**
     * 根据当前用户来修改密码
     * @return
     */
    @GetMapping("/updatePassword")
    public R updatePasswordByUser(@RequestParam(value = "oldPassword",required = false) String oldPassword,
                                  @RequestParam(value = "newPassword",required = false) String newPassword,
                                  @RequestParam(value = "qyPassword",required = false) String qyPassword) {
        return indexService.updatePasswordByUser(oldPassword,newPassword,qyPassword);
    }

    /**
    * @Description: 根据设备编号管理员强制修改密码
    * @Param: [deviceNumber, newPassword, qyPassword]
    * @return: com.cqupt.scanning.system.utils.result.R
    * @Author: Jack Wong
    * @Date: 2022-5-27
    */
    @GetMapping("/forceUpdatePassword")
    public R forceUpdatePassword(@RequestParam(value = "deviceNumber",required = false) String deviceNumber,
                                 @RequestParam(value = "deviceType",required = false) String deviceType,
                                  @RequestParam(value = "newPassword",required = false) String newPassword,
                                  @RequestParam(value = "qyPassword",required = false) String qyPassword) {
        return indexService.forceUpdatePassword(deviceNumber,deviceType,newPassword,qyPassword);
    }
}
